package org.example.mybatis.mapper;

import org.apache.ibatis.annotations.*;
import org.example.mybatis.model.UserInfo;

import java.util.List;

@Mapper
public interface IUserInfoMapper {
    @Select("select *FROM userinfo ")
    List<UserInfo> getUserInfoAll();

    @Select("SELECT *from userinfo where age=#{age}")
    List<UserInfo> getUserInfo1(Integer age);

    @Select("SELECT * from userinfo where username = #{username}")
    UserInfo getUserInfoUsername(String username);

    @Select("SELECT *from userinfo where gender = #{gender1} and age = #{age}")
    List<UserInfo> getUserInfo2(@Param("gender1") Integer gender, Integer age);//相当于起别名

    @Select("SELECT *from userinfo where id=#{id}")
    List<UserInfo> getUserInfo3(@Param("id") Integer id);

    @Select("SELECT *from userinfo where id=#{id}")
    UserInfo getUserInfo4(@Param("id") Integer id);
    //当返回一个结果的时候，可以使用对象或者集合来接受，如果存在多个返回值，那么就只能选择集合接收


    @Insert("insert into userinfo (username,password,age,gender) values (#{username},#{password},#{age},#{gender})")
    Integer insertUserInfo1(UserInfo userInfo);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender) values (#{username},#{password},#{age},#{gender})")
    Integer insertUserInfo2(UserInfo userInfo);


    //使用@Param注解，会将这些看成一个整体
    @Insert("insert into userinfo (username,password,age,gender) values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})")
    Integer insertUserInfo3(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from userinfo where id = #{id}")
    Integer deleteUserInfo1(UserInfo userInfo);

    @Update("update userinfo set password = #{password},gender = #{gender} where id = #{id}")
    Integer upsetUserInfo1(UserInfo userInfo);

    /*
    * 起别名方式
    * */
    @Select("SELECT id,username,password,age,gender,phone," +
            "delete_flag as deleteFlag,create_time as createTime,update_time as updateTime from userinfo ;")
    List<UserInfo> getUserInfoAll1();

    /*
    * 使用注释
    * */

    @Results(id = "resultMap",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select *from userinfo")
    List<UserInfo> getUserInfoAll2();

    //直接使用映射的id，即可直接使用
    @ResultMap("resultMap")
    @Select("select *from userinfo")
    List<UserInfo> getUserInfoAll3();

    /*
    * 在配置文件中，开始驼峰命名
    * */

    /*
    * 模糊查询
    * */

    @Select("SELECT * from userinfo where username Like concat('%',#{username},'%')")
    List<UserInfo> getUserinfoLike(String username);
}
